package com.skyward.android.androiddemo2.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.skyward.android.androiddemo2.interfaces.IBaseWeatherData;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by skyward on 2017/4/25.
 * email:
 */

public class FifteenFutureWeather<T extends IBaseWeatherData> extends View {


    private int maxTemperature;
    private int minTemperature;

    private Paint mPaint;
    private List<T> mList = new ArrayList<>();
    private int position;
    private int degreeHeight;
    private int mHeight;
    private int mWidth;
    private int radius = 2;
    private int marginTop =30;
    private int marginBottom=30;

    public FifteenFutureWeather(Context context) {
        super(context);
        init();
    }

    public FifteenFutureWeather(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public FifteenFutureWeather(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.RED);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(getDefaultSize(60,widthMeasureSpec),getDefaultSize(100,heightMeasureSpec));
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mHeight = getMeasuredHeight()-marginTop-marginBottom;
        mWidth = getMeasuredWidth();

        Log.i("myLog","height: "+mHeight);

        //在计算出温度线所在的区间后，计算每一度的高度
        degreeHeight = mHeight / (maxTemperature - minTemperature);
        T t = mList.get(position);

        Point[] tPoints = getTPoints(t);

        drawPoints(canvas,tPoints);
        drawText(canvas,tPoints,t);
        //画左边连线
        if(position > 0){
            T  yesterdayTemp = mList.get(position -1);
            Point[] leftPoints = getLeftPoints(tPoints,yesterdayTemp);
            drawLine(canvas,tPoints,leftPoints);
        }
       //右边
        if(position < mList.size()-1){
            T tomorrowTemp = mList.get(position+1);
            Point[] rightPoints = getRightPoints(tPoints,tomorrowTemp);
            drawLine(canvas,tPoints,rightPoints);
        }


    }

    private void drawText(Canvas canvas, Point[] tPoints,T t) {
        Rect dayTempRect = new Rect();
        Rect nightTempRect = new Rect();
        String dayTempStr = String.valueOf(t.getDayTemp())+"°";
        String nightTemStr = String.valueOf(t.getNightTemp())+"°";
        mPaint.getTextBounds(dayTempStr,0,dayTempStr.length(),dayTempRect);
        canvas.drawText(dayTempStr,tPoints[0].x-dayTempRect.width()/2,tPoints[0].y-10,mPaint);
        mPaint.getTextBounds(nightTemStr,0,nightTemStr.length(),nightTempRect);
        canvas.drawText(nightTemStr,tPoints[1].x-nightTempRect.width()/2,tPoints[1].y+15,mPaint);
    }


    private void drawLine(Canvas canvas, Point[] tPoints, Point[] points) {
        canvas.drawLine(tPoints[0].x,tPoints[0].y,points[0].x,points[0].y,mPaint);
        canvas.drawLine(tPoints[1].x,tPoints[1].y,points[1].x,points[1].y,mPaint);
    }

    private Point[] getLeftPoints(Point[] tPoints, T yesterdayTemp) {
        Point[] points = new Point[2];
        Point[] yesterdayPoints = getTPoints(yesterdayTemp);
        points[0] = new Point(0,(tPoints[0].y + yesterdayPoints[0].y) /2);
        points[1] = new Point(0,(tPoints[1].y+yesterdayPoints[1].y)/2);
        return points;
    }

    private Point[] getRightPoints(Point[] tPoints, T tomorrowTemp) {
        Point[] points = new Point[2];
        Point[] tomorrowPoints = getTPoints(tomorrowTemp);
        points[0] = new Point(mWidth,(tPoints[0].y+tomorrowPoints[0].y)/2);
        points[1] = new Point(mWidth,(tPoints[1].y+tomorrowPoints[1].y)/2);
        return points;
    }

    private void drawPoints(Canvas canvas, Point[] tpoints) {
        canvas.drawCircle(tpoints[0].x,tpoints[0].y,radius,mPaint);
        canvas.drawCircle(tpoints[1].x,tpoints[1].y,radius,mPaint);
    }

    private Point[] getTPoints(T t) {
        Point[] points = new Point[2];
        int DayY = marginTop + (maxTemperature - t.getDayTemp())*degreeHeight;
        Point dayHeight = new Point(mWidth/2,DayY);
        int nightY = marginBottom + (maxTemperature - t.getNightTemp())*degreeHeight;
        Point nightHeight = new Point(mWidth/2,nightY);
        points[0] = dayHeight;
        points[1] = nightHeight;
        return points;
    }

    public void setData(List<T> list,int position){
        this.mList = list;
        this.position = position;
        invalidate();
    }



    public void setMaxTemperature(int maxTemperature) {
        this.maxTemperature = maxTemperature;
    }

    public void setMinTemperature(int minTemperature) {
        this.minTemperature = minTemperature;
    }
}
